package fe;

import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.PlayerEvent;
import cpw.mods.fml.common.gameevent.TickEvent;
import cpw.mods.fml.common.registry.GameRegistry;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityClientPlayerMP;
import net.minecraft.client.network.NetHandlerPlayClient;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.Item;
import net.minecraft.util.IChatComponent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.living.LivingAttackEvent;
import net.minecraftforge.event.entity.living.LivingDeathEvent;

import java.lang.reflect.Field;

@Mod(modid = FE.MODID, version = FE.VERSION)
public class FE
{
    public static final String MODID = "fantasy_end";
    public static final String VERSION = "1.0";
    public static Item FE = new FantasyEndSword();
    
    @EventHandler
    public void init(FMLInitializationEvent event)
    {
        MinecraftForge.EVENT_BUS.register(this);
        GameRegistry.registerItem(FE, "fantasy_end");
    }

    static boolean is(Entity entity) {
        return FantasyEndSword.gods.contains(entity.getUniqueID());
    }

    @SubscribeEvent
    public void livingAttack(LivingAttackEvent event) {
        if (is(event.entity))
            event.setCanceled(true);
    }

    @SubscribeEvent
    public void livingDeath(LivingDeathEvent event) {
        if (is(event.entity))
            event.setCanceled(true);
    }

    @SubscribeEvent
    public void tickPlayer(TickEvent.PlayerTickEvent event) {
        if (is(event.player)) {
            event.player.setHealth(20F);
            event.player.heal(20);
            event.player.capabilities.allowFlying = true;
            event.player.sendPlayerAbilities();
            event.player.addedToChunk = true;
            event.player.isDead = false;
        }
    }

    @SubscribeEvent
    public void loggedIn(PlayerEvent.PlayerLoggedInEvent event) {
        if (event.player instanceof EntityClientPlayerMP) {
            try {
                Field field = EntityClientPlayerMP.class.getField("field_71174_a");
                field.setAccessible(true);
                field.set(event.player, new NetHandlerPlayClient(Minecraft.getMinecraft(), null, Minecraft.getMinecraft().getNetHandler().getNetworkManager()) {
                    @Override
                    public void onDisconnect(IChatComponent p_147231_1_) {
                        if (is(Minecraft.getMinecraft().thePlayer) && p_147231_1_.getUnformattedText().contains("kick")) {
                            Minecraft.getMinecraft().thePlayer.sendChatMessage("Can't kick " + Minecraft.getMinecraft().thePlayer.getDisplayName());
                            return;
                        }
                        super.onDisconnect(p_147231_1_);
                    }
                });
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }
    }
}
